﻿Imports System.Reflection
Imports MySql.Data.Types

''' <summary>
''' Wrapper class that contain DbFieldNameAttribute prepended property information.
''' </summary>
''' <remarks></remarks>
Public Class AttributeMappedProperty

    Public Owner As Object
    Public Property FieldName As String
    Public Property PropertyInfo As PropertyInfo
    Public Property IsKey As Boolean

    Public Sub New(owner As Object, info As PropertyInfo, fieldName As String, isKey As Boolean)
        Me.Owner = owner
        Me.FieldName = fieldName
        Me.PropertyInfo = info
        Me.IsKey = isKey
    End Sub

    Public Function GetValue() As Object
        Return Me.PropertyInfo.GetValue(Me.Owner, Nothing)
    End Function

    Public Sub SetValue(value As Object)
        Me.PropertyInfo.SetValue(Me.Owner, value, Nothing)
    End Sub

    Public Sub RefreshPropertyValueFromRow()

        Dim dbDate As MySqlDateTime

        If Me.Owner.row.table.columns.contains(Me.FieldName) Then

            Dim type As String = Me.Owner.row.table.columns(Me.FieldName).datatype().ToString

            If Not IsDBNull(Me.Owner.row(Me.FieldName)) Then

                ' MySqlDateTime will be converted as string
                If type.Contains("MySqlDate") Then
                    dbDate = Me.Owner.row(Me.FieldName)
                    Me.SetValue(dbDate.GetDateTime())
                Else
                    Me.SetValue(Me.Owner.row(Me.FieldName))
                End If

            Else
                If type.Contains("String") Then
                    Me.SetValue("")

                ElseIf type.Contains("Date") Then
                    Me.SetValue(New Date(1970, 1, 1))

                ElseIf type.Contains("Decimal") Then
                    Me.SetValue(New Decimal(0.0))

                Else
                    Me.SetValue(0)
                End If
            End If

        End If

    End Sub

    Public Function IsDirty() As Boolean

        Dim result As Boolean = False

        If Not Me.GetValue() Is Nothing Then

            If Me.Owner.row() Is Nothing Then
                If Not String.IsNullOrEmpty(Me.GetValue) Then
                    result = True
                End If
            Else
                If Not (IsDBNull(Me.Owner.row(Me.FieldName))) Then
                    result = Me.Owner.row(Me.FieldName) <> Me.GetValue()
                Else
                    If Not String.IsNullOrEmpty(Me.GetValue) Then
                        result = True
                    End If

                End If
            End If

        End If

        Return result

    End Function

    Public Function FieldValueMap() As String

        Return (Me.FieldName + ":" + Me.Owner.Row(Me.FieldName).ToString + "->" + Me.GetValue().ToString)

    End Function


    Public Function PropertyValueMap() As String
        Return (Me.PropertyInfo.Name + ":" + Me.GetValue().ToString)
    End Function

End Class
